package com.tangkun.environment.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tangkun.environment.R;
import com.tangkun.environment.activity.WebZiXunActivity;
import com.tangkun.environment.entity.xinwen.NewsReturn;
import com.tangkun.environment.network.GlobalUrl;
import com.tangkun.environment.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author tangkun
 * @ClassName: XinWenAdapter
 * @Description: 首页新闻列表适配器
 * @date 2016-08-20 上午11:57:36
 */
public class XinWenAdapter extends BaseAdapter {

    private Context context;
    private List<NewsReturn.ResultBean.ListBean> listData = new ArrayList<NewsReturn.ResultBean.ListBean>();

    public XinWenAdapter(Context context, List<NewsReturn.ResultBean.ListBean> listData) {
        this.context = context;
        this.listData = listData;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int i) {
        return listData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_xinwen, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (listData != null) {
            //图片
            Glide.with(context)
                    .load(GlobalUrl.IMG_URL + listData.get(position).img)
                    .dontAnimate()
                    .placeholder(R.mipmap.ic_place_holder)
                    .into(holder.tvXinwenImg);
            // 内容
            if (!TextUtils.isEmpty(listData.get(position).title)) {
                holder.tvXinwenContent.setText(listData.get(position).title);
            } else {
                holder.tvXinwenContent.setText("");
            }
            // 日期
            if (!TextUtils.isEmpty(listData.get(position).createtime)) {
                holder.tvXinwenRiqi.setText(Utils.getDateYearMonthDay(listData.get(position).createtime));
            } else {
                holder.tvXinwenRiqi.setText("");
            }
            // 类别
            if (!TextUtils.isEmpty(listData.get(position).typename)) {
                holder.tvXinwenLeibie.setText(listData.get(position).typename);
            } else {
                holder.tvXinwenLeibie.setText("");
            }
            //跳转新闻详情
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, WebZiXunActivity.class);
                    intent.putExtra("id", listData.get(position).ids);//ID
                    context.startActivity(intent);
                }
            });
        }
        return convertView;

    }


    static class ViewHolder {
        @BindView(R.id.tv_xinwen_img)
        ImageView tvXinwenImg;
        @BindView(R.id.tv_xinwen_content)
        TextView tvXinwenContent;
        @BindView(R.id.tv_xinwen_riqi)
        TextView tvXinwenRiqi;
        @BindView(R.id.tv_xinwen_leibie)
        TextView tvXinwenLeibie;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
